package com.xqsight.account.controller;

import com.google.common.collect.Lists;
import com.xqsight.account.entity.SysArea;
import com.xqsight.account.service.impl.SysAreaServiceImpl;
import com.xqsight.api.account.dto.SysAreaDTO;
import com.xqsight.api.account.openfeign.SysAreaFeign;
import com.xqsight.common.response.ResponseData;
import com.xqsigth.common.core.utils.BeanConvertUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 〈一句话功能简述〉<br>
 * 〈功能详细描述〉
 *
 * @author ganggang.wang
 * @see [相关类/方法]（可选）
 * @since 2020/4/30
 */
@RestController
public class SysAreaController implements SysAreaFeign {

    @Autowired
    private SysAreaServiceImpl sysAreaService;

    @Override
    public ResponseData insert(SysAreaDTO sysAreaDTO) {
        SysArea sysArea = BeanConvertUtils.copyProperties(sysAreaDTO, SysArea.class);
        return ResponseData.SUCCESS(sysAreaService.save(sysArea));
    }

    @Override
    public ResponseData update(SysAreaDTO sysAreaDTO) {
        SysArea sysArea = BeanConvertUtils.copyProperties(sysAreaDTO, SysArea.class);
        return ResponseData.SUCCESS(sysAreaService.updateById(sysArea));
    }

    @Override
    public ResponseData delete(String areaCode) {
        return ResponseData.SUCCESS(sysAreaService.removeById(areaCode));
    }

    @Override
    public ResponseData get(String areaCode) {
        SysAreaDTO sysAreaDTO = BeanConvertUtils.copyProperties(sysAreaService.getById(areaCode), SysAreaDTO.class);
        return ResponseData.SUCCESS(sysAreaDTO);
    }

    @Override
    public ResponseData list(SysAreaDTO sysAreaDTO) {
        SysArea sysArea = BeanConvertUtils.copyProperties(sysAreaDTO, SysArea.class);
        List<SysArea> sysAreas = sysAreaService.list(sysArea);
        List<SysAreaDTO> sysAreaDTOS = Optional.ofNullable(sysAreas).orElseGet(Lists::newArrayList).stream()
                .map(x -> BeanConvertUtils.copyProperties(x, SysAreaDTO.class)).collect(Collectors.toList());
        return ResponseData.SUCCESS(sysAreaDTOS);
    }
}
